package com.zhentao.controller;

import com.zhentao.pojo.Student;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @Date 2024/2/23 8:49
 * @Author xdm
 **/
@Controller
public class HelloController {
    @GetMapping("/basic")
    public String test(Model model){
        model.addAttribute("name","测试名字");

        Student student=new Student();
        student.setName("小明");
        student.setAge(19);
        model.addAttribute("student",student);

        return "01-basic";
    }

    @GetMapping("/list")
    public String list(Model model){

        //------------------------------------
        Student stu1 = new Student();
        stu1.setName("小强");
        stu1.setAge(18);
        stu1.setMoney(1000.86f);
        stu1.setBirthday(new Date());

        //小红对象模型数据
        Student stu2 = new Student();
        stu2.setName("小红");
        stu2.setMoney(200.1f);
        stu2.setAge(19);

        Student stu3 = new Student();
        stu3.setName("小明");
        stu3.setMoney(200.1f);
        stu3.setAge(19);

        //将两个对象模型数据存放到List集合中
        List<Student> stus = new ArrayList<>();
        stus.add(stu1);
        stus.add(stu2);
        stus.add(stu3);

        //向model中存放List集合数据
        model.addAttribute("stus",stus);

        //------------------------------------

        //创建Map数据
        HashMap<String,Student> stuMap = new HashMap<>();
        stuMap.put("stu1",stu1);
        stuMap.put("stu2",stu2);
        // 3.1 向model中存放Map数据
        model.addAttribute("stuMap", stuMap);

        return "01-basic";
    }


    @Autowired
    private Configuration configuration;
    @ResponseBody
    @GetMapping("/createHtml")
    public String createHtml() throws IOException, TemplateException {
        Template template=configuration.getTemplate("01-basic.ftl");

        //------------------------------------
        Student stu1 = new Student();
        stu1.setName("小强");
        stu1.setAge(18);
        stu1.setMoney(1000.86f);
        stu1.setBirthday(new Date());

        //小红对象模型数据
        Student stu2 = new Student();
        stu2.setName("小红");
        stu2.setMoney(200.1f);
        stu2.setAge(19);

        Student stu3 = new Student();
        stu3.setName("小明");
        stu3.setMoney(200.1f);
        stu3.setAge(19);

        //将两个对象模型数据存放到List集合中
        List<Student> stus = new ArrayList<>();
        stus.add(stu1);
        stus.add(stu2);
        stus.add(stu3);

        //创建Map数据
        HashMap<String,Object> stuMap = new HashMap<>();
        stuMap.put("stu1",stu1);
        stuMap.put("stu2",stu2);
        stuMap.put("stus",stus);
        //param必须是map,ftl展示数据的时候直接使用map里面的属性就行 -eg.${stu1}
        template.process(stuMap,new FileWriter("D://list.html"));

        return "success";

    }
}
